from database.database import Database
from model.dbmodel import DbParkRecord
from datetime import datetime


class ParkRecordTable(Database):
    def __init__(self):
        self.table_name = "park_records"

    def insert(self, info: DbParkRecord, commit=False):
        sql = self.get_insert_sql_from_obj(info, self.table_name)
        self.execute(sql)
        if commit:
            self.commit()
        return int(self.cursor.lastrowid)

    def query(self, id: int) -> DbParkRecord:
        sql = f"select * from {self.table_name} where `id` = {id}"
        self.execute(sql)
        result = self.cursor.fetchone()
        if not result:
            return None
        return DbParkRecord().load(result)
